package com.qinge.blog.utils;

import com.qinge.blog.entity.BlogUser;
import com.qinge.blog.service.BlogUserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author QDW
 * @date 2022/4/29 15:09
 * @description CustomizeIAuthenticationFacade
 **/
public class CurrentUserUtil {


    /**
     * @author QDW
     * @date 2022/5/2 22:20
     * @description 获取用户id
     * @param
     * @return java.lang.Integer
     **/
    public static  String getUserId() {
        SecurityContext sc = SecurityContextHolder.getContext();

        Authentication auth = sc.getAuthentication();

        BlogUser user = (BlogUser) auth.getPrincipal();

        return user.getId();
    }

    /**
     * @author QDW
     * @date 2022/5/2 22:20
     * @description 获取账号
     * @param
     * @return java.lang.String
     **/
    public static String getUserAccount() {
        SecurityContext sc = SecurityContextHolder.getContext();

        Authentication auth = sc.getAuthentication();

        return auth.getName();
    }

    public static boolean isAdmin(){
        BlogUser user = (BlogUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if(user.getAuthorities().size()>0&&user.getAuthorities().stream().map(e->e.getAuthority()).collect(Collectors.toList()).contains("admin")){
            return true;
        }

        return false;
    }


}
